<?

function conference_history() {
	global $user;
	
	ippbx_set_db();
	
	$user_confs = db_query('SELECT id,name FROM Conference WHERE ID_User=%d', $user->uid);
	$user_conf_data = array();
	$where = '0';
	
	while ( $row = db_fetch_array($user_confs) ) {
		$user_conf_data[]['id'] = $row['id'];
		$user_conf_data[]['name'] = $row['name'];
		$where .= ','.$row['id'];
	}
	
	$history = db_query( 'SELECT h.id, h.create_date, h.startup_date, h.duration, h.action_status, h.error_code, h.conf_id, (SELECT name FROM Conference WHERE id=h.conf_id) AS name FROM Conf_history h WHERE conf_id IN(%s) ORDER BY id', $where);
	ippbx_unset_db();
	
	$hist = array();
	$i = 0;
	
	while ( $row = db_fetch_array( $history ) ) {
		$hist[$i]['name'] = l( $row['name'], 'ippbx/conference/'.$row['conf_id'].'/edit' );
		$hist[$i]['create_date'] = $row['create_date'];
		$hist[$i]['startup_date'] = $row['startup_date'];
		$hist[$i]['duration'] = $row['duration'];
		$hist[$i]['action_status'] = conference_status($row['action_status']);
		$hist[$i]['error_code'] = conference_error_code($row['error_code']);
		$hist[$i]['more'] = l( 'Детально', 'ippbx/conference/history/'.$row['id'].'/view');
		$i++;
	}
	
	$headers = array( t('Название'), t('Время сбора'), t('Время начала'), t('Длительность сек.'), t('Состояние'), t('Код'), t('Функции'));
	if ( $hist == NULL ) {
		$hist[0][0] = array(
			'data' => t('Извините, ваша история конференций пуста.'),
			'colspan' => 7,
		);
	}

	return theme('table', $headers, $hist, array('style' => 'width: 100%; text-align: center;'));
}

function conference_error_code( $ecode ) {
	$error = array(
		'100'=>t('сбор конференции'),
		'101'=>t('начало конференции'),
		'102'=>t('будильник установлен вручную секретарем(активирован)'),
		'196'=>t('IP-адрес изменен успешно'),
		'197'=>t('конференция (все ее алармы) была удалена'),
		'198'=>t('будильник сброшен (удален)'),
		'199'=>t('конф-я состоялась'),
		'201'=>t('вход абонента в конфу (№ абонента)'),
		'202'=>t('выход абонента из конфы'),
		'301'=>t('запрос на поиск конференции в БД по номеру активации'),
		'302'=>t('???'),
		'303'=>t('конференция не активирована'),
		'304'=>t('не набралось достаточного кол-ва участников конфы'),
		'305'=>t('данная конфа уже была создана'),
	);
	return $error[$ecode];
}

function conference_status( $sid ) {
	$status = array(
		'1' => t('Сбор'),
		'2' => t('Разговор'),
		'3' => t('Завершена'),
	);
	return $status[$sid];
}

function conference_history_view( $hid ) {

	ippbx_set_db();
	$result = db_query("SELECT c.conf_id AS conf_id, (SELECT name FROM Conference WHERE id=c.conf_id) AS name, action_status, create_date, startup_date, duration, error_code, answered_users, noanswered_users, answered_users_aliases, noanswered_users_aliases FROM Conf_history c WHERE id=%d", $hid);
	$conf_hist = db_fetch_array( $result );
	ippbx_unset_db();
	
	$conf_hist['conf'] = l( $conf_hist['name'] , 'ippbx/conference/'.$conf_hist['conf_id'].'/edit' );
	$conf_hist['action_status'] = conference_status( $conf_hist['action_status'] );
	$conf_hist['message'] = conference_error_code( $conf_hist['error_code'] );
	
	$conf_hist['answered_users'] = explode(';', $conf_hist['answered_users']);
	$conf_hist['noanswered_users'] = explode(';', $conf_hist['noanswered_users']);
	$conf_hist['answered_users_aliases'] = explode(';', $conf_hist['answered_users_aliases']);
	$conf_hist['noanswered_users_aliases'] = explode(';', $conf_hist['noanswered_users_aliases']);
	
	$conf_hist['answered_users'] = conference_clear_users($conf_hist['answered_users']);
	$conf_hist['noanswered_users'] = conference_clear_users($conf_hist['noanswered_users']);
	$conf_hist['answered_users_aliases'] = conference_clear_users($conf_hist['answered_users_aliases']);
	$conf_hist['noanswered_users_aliases'] = conference_clear_users($conf_hist['noanswered_users_aliases']);
	
	$ans_users = array();
	$no_ans_users = array();
	
	for ( $i = 0; $i < count( $conf_hist['answered_users'] ); $i++ ) {
		$ans_users[$i]['alias'] = $conf_hist['answered_users_aliases'][$i];
		$ans_users[$i]['num'] = $conf_hist['answered_users'][$i];
	}
	
	for ( $i = 0; $i < count( $conf_hist['noanswered_users'] ); $i++ ) {
		$no_ans_users[$i]['alias'] = $conf_hist['noanswered_users_aliases'][$i];
		$no_ans_users[$i]['num'] = $conf_hist['noanswered_users'][$i];
	}
	
	$headers = array( t('Имя') , t('Номер') );
	
	$ans_users = theme('table' , $headers, $ans_users);
	$no_ans_users = theme('table', $headers, $no_ans_users);
//array('conference' => NULL, 'conf_create' => NULL, 'conf_startup' => NULL, 'duration' => NULL, 'status' => NULL, 'message' => NULL, 'ans_users' => NULL, 'no_ans_users' => NULL),
	return theme('conference_history_view', $conf_hist['conf'], $conf_hist['create_date'], $conf_hist['startup_date'], $conf_hist['duration'], $conf_hist['action_status'], $conf_hist['message'], $ans_users, $no_ans_users);
}

function conference_clear_users( $array ) {
	$key = array_search( NULL, $array);
	if ( $key != FALSE ) {
		unset($array[$key]);
	}
	return $array;
}
?>